<?php
namespace app\admin\controller;

use app\common\model\Report as ReportModel;
use app\common\model\Topic as TopicModel;
use app\common\model\Member as MemberModel;
/**
 * 举报控制器
 * Class Report
 * @package app\admin\controller
 */
class Report extends Base
{
    /**
     * [getIndex  举报列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $data = input('param.');
        $model = new ReportModel();
        $tmodel = new TopicModel();
        $mmodel = new MemberModel();
        $this->getPageAndSize($data);
        $map = [];
        $user = $this->user;
        try{
            $field = 'r.id,r.content,r.status,r.topic_id tid,r.addtime,am.username,am.tel,at.content tcontent';
            $map['at.states'] = ['neq',2];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,$field);
            foreach($lists as &$v){
                $v['tcontent'] = urldecode($v['tcontent']);
                $topic = $tmodel->findData($v['tid'],'id','id,memid,parid,comment_uid,addtime');
                $v['taddtime'] = date('Y-m-d H:i:s',$topic['addtime']);
                if(empty($topic['parid'])){
                    $mem = $mmodel->findData($topic['memid'],'id','username');
                    $v['tusername'] = $mem['username'];
                }else{
                    $mem = $mmodel->findData($topic['comment_uid'],'id','username');
                    $v['tusername'] = $mem['username'];
                }
         
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog('查看举报列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postEdit  禁止]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new ReportModel();
        $tmodel = new TopicModel();
        try {
            $tmodel->editData(['states' => 2, 'id' => $param['tid']]);
            $model->changeStatus(['id' => $param['id']], 1);
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '禁止成功');
    }
}